#include "launcher.h"
#include "ui_launcher.h"
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QGridLayout>
#include <QMessageBox>
#include <QtWidgets/QDesktopWidget>
#include "backlight.h"
#include "statusled.h"
#include "wdog.h"
#include "beep.h"
#include "e2prom.h"
#include "parallel.h"
#include "launcher.h"
#include "audio.h"
#include "net.h"
#include "startset.h"
#include "date.h"

#define PNG_BUTTON_COLUMN_SPACING 80
#define PNG_BUTTON_ROW_SPACING  10
Launcher::Launcher(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Launcher)
{
     SetMainBacklight();
     QGridLayout  *mainGridLayout= new QGridLayout;
     QHBoxLayout *oneHBoxLayout = new QHBoxLayout;
     QHBoxLayout *twoHBoxLayout = new QHBoxLayout;
     QHBoxLayout *threeHBoxLayout = new QHBoxLayout;

     QToolButton *backlightBtn = new   QToolButton(this);
     QToolButton *ledBtn = new   QToolButton(this);
     QToolButton *wdtBtn = new   QToolButton(this);
     QToolButton *beepBtn = new   QToolButton(this);
     QToolButton *dateBtn = new   QToolButton(this);
     QToolButton *musicBtn = new   QToolButton(this);
     //QToolButton *fileBrowserBtn = new   QToolButton(this);
     QToolButton *browserBtn = new   QToolButton(this);
     QToolButton *settingBtn = new   QToolButton(this);
     QToolButton *eepromBtn = new   QToolButton(this);
     QToolButton *ioBtn = new   QToolButton(this);
     QToolButton *serialBtn = new   QToolButton(this);
     QToolButton *CalibrateBtn = new   QToolButton(this);

     SetPngButton(backlightBtn,":/launcher/png/png/backlight.png","Backlight");
     SetPngButton(ledBtn,":/launcher/png/png/led.png","Led");
     SetPngButton(wdtBtn,":/launcher/png/png/wdt.png","WDog");
     SetPngButton(beepBtn,":/launcher/png/png/beep.png","Beep");
SetPngButton(dateBtn,":/launcher/png/png/ping.png","TimeSet");
     SetPngButton(musicBtn,":/launcher/png/png/music.png","Music");
     SetPngButton(browserBtn,":/launcher/png/png/browser.png","NetSet");
     SetPngButton(settingBtn,":/launcher/png/png/setting.png","StartSet");
     SetPngButton(eepromBtn,":/launcher/png/png/eeprom.png","E2prom");
     SetPngButton(ioBtn,":/launcher/png/png/io.png","IO");
     SetPngButton(serialBtn,":/launcher/png/png/serial.png","Serial");
     SetPngButton(CalibrateBtn,":/launcher/png/png/can.png","Calibrate");

     oneHBoxLayout->addWidget(backlightBtn,0);
     oneHBoxLayout->addWidget(ledBtn,1);
     oneHBoxLayout->addWidget(wdtBtn,2);
     oneHBoxLayout->addWidget(beepBtn,3);

     oneHBoxLayout->setSpacing(PNG_BUTTON_COLUMN_SPACING);

     twoHBoxLayout->addWidget(dateBtn,0);
     twoHBoxLayout->addWidget(musicBtn,1);
     twoHBoxLayout->addWidget(browserBtn,2);
     twoHBoxLayout->addWidget(settingBtn,3);

     twoHBoxLayout->setSpacing(PNG_BUTTON_COLUMN_SPACING);

     threeHBoxLayout->addWidget(eepromBtn,0);
     threeHBoxLayout->addWidget(ioBtn,1);
     threeHBoxLayout->addWidget(serialBtn,2);
     threeHBoxLayout->addWidget(CalibrateBtn,3);

     threeHBoxLayout->setSpacing(PNG_BUTTON_COLUMN_SPACING);

     mainGridLayout->addLayout(oneHBoxLayout,0,0,1,1);
     mainGridLayout->addLayout(twoHBoxLayout,1,0,1,1);
     mainGridLayout->addLayout(threeHBoxLayout,2,0,1,1);

     mainGridLayout->setSpacing(PNG_BUTTON_ROW_SPACING);
     mainGridLayout->setAlignment(Qt::AlignCenter);
     this->setLayout(mainGridLayout);
     //this->showFullScreen();
     this->showNormal();
     connect(backlightBtn,SIGNAL(clicked()),this,SLOT(Backlight_clicked()));
     connect(ledBtn,SIGNAL(clicked()),this,SLOT(Led_clicked()));
     connect(wdtBtn,SIGNAL(clicked()),this,SLOT(WDog_clicked()));
     connect(beepBtn,SIGNAL(clicked()),this,SLOT(Beep_clicked()));
     connect(eepromBtn,SIGNAL(clicked()),this,SLOT(E2Prom_clicked()));
     connect(ioBtn,SIGNAL(clicked()),this,SLOT(Parallel_clicked()));
     connect(musicBtn,SIGNAL(clicked()),this,SLOT(Music_clicked()));
     connect(browserBtn,SIGNAL(clicked()),this,SLOT(Browser_clicked()));
     connect(serialBtn,SIGNAL(clicked()),this,SLOT(Serial_clicked()));
     connect(CalibrateBtn,SIGNAL(clicked()),this,SLOT(Calibrate_clicked()));
     connect(dateBtn,SIGNAL(clicked()),this,SLOT(date_clicked()));
     connect(settingBtn,SIGNAL(clicked()),this,SLOT(Setting_clicked()));
     ui->setupUi(this);
}
void Launcher::SetMainBacklight()
{
    QPalette bgpal = palette();
    bgpal.setColor (QPalette::Background, QColor (0, 0 , 0, 255));
    bgpal.setColor (QPalette::Foreground, QColor (255,0,0,255));
    setPalette (bgpal);
}
void Launcher::SetPngButton(QToolButton *toolButton,QString buttonPng,QString buttonName)
{
    toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    toolButton->setIcon(QPixmap(buttonPng));
    toolButton->setIconSize(QPixmap(buttonPng).size());
    toolButton->setAutoRaise(true);
    toolButton->setText(buttonName);
}

Launcher::~Launcher()
{
    delete ui;
}

void Launcher::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Launcher::Backlight_clicked()
{
    //QMessageBox::information(NULL,"Backlight","backlight was pressed!!!",QMessageBox::Yes,QMessageBox::Yes);
    Backlight a;


    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
   // a.setWindowFlags(Qt::WindowTitleHint|Qt::CustomizeWindowHint);
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();
}
void Launcher::Led_clicked()
{
    Statusled a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
   // a.setWindowFlags(Qt::WindowTitleHint|Qt::CustomizeWindowHint);
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();
}
void Launcher::WDog_clicked()
{
    WDog a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
   // a.setWindowFlags(Qt::WindowTitleHint|Qt::CustomizeWindowHint);
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();

}
void Launcher::Beep_clicked()
{
    Beep a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
   // a.setWindowFlags(Qt::WindowTitleHint|Qt::CustomizeWindowHint);
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();
}
void Launcher::E2Prom_clicked()
{
    E2Prom a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
   // a.setWindowFlags(Qt::WindowTitleHint|Qt::CustomizeWindowHint);
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();
}
void Launcher::Parallel_clicked()
{
    Parallel a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
   // a.setWindowFlags(Qt::WindowTitleHint|Qt::CustomizeWindowHint);
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();
}
void Launcher::Music_clicked()
{
    Audio a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();  
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();
}
void Launcher::Browser_clicked()
{  
    Net a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
    a.move((width-a.width())/2,(height-a.height())/2);
    a.exec();
}
void Launcher::Serial_clicked()
{
    //QProcess *process = new QProcess;
    //this->showMinimized();
    //system("killall -15 SysLauncher");
   // process->execute("/bin/qserial");
   // system("/yctek/app/SysLauncher");
    system("killall -15 SysLauncher");
    system("/bin/qserial");
    system("/yctek/app/SysLauncher");

}
void Launcher::Calibrate_clicked()
{
        int a;
        this->showMinimized();
        system("rm /etc/pointercal");
        a=system("/bin/yccalibrate");
        this->showFullScreen();
}
void Launcher::date_clicked()
{
    Date a;
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
    a.move((width-a.width())/2,(height-a.height())/2);
   a.exec();
}
void Launcher::Setting_clicked()
{
       Startset a;
       QDesktopWidget *desktop=QApplication::desktop();
       int width=desktop->width();
       int height=desktop->height();
       a.move((width-a.width())/2,(height-a.height())/2);
      a.exec();
}
